package www.bancodelanacion.intranet.prestamos.controlador;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.bancodelanacion.intranet.prestamos.entidad.IfiDTO;
import www.bancodelanacion.intranet.prestamos.service.IfiService;
import www.bancodelanacion.intranet.prestamos.service.IfiServiceImpl;

/**
 * Servlet implementation class ServletIfi
 */
public class ServletIfi extends HttpServlet {
	private static final long serialVersionUID = 1L;

	IfiService servicio=new IfiServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String metodo = request.getParameter("metodo");
		if (metodo.equals("buscaIFIporcodigo")) {
			buscaIFIporcodigo(request, response);
			lista(request, response);
		} else if (metodo.equals("buscaIFIportipo")) {
			buscaIFIportipo(request, response);
			lista(request, response);
		} else if (metodo.equals("lista")) {
			lista(request, response);
		}else if (metodo.equals("buscarCodigo")) {
			buscarCodigo(request, response);
		}
	}
	
	protected void buscaIFIporcodigo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String codigo = request.getParameter("codigo");
			IfiDTO bean = servicio.buscaIFIporcodigo(codigo);
			request.setAttribute("ifi", bean);
			request.getRequestDispatcher("/buscarIFI.jsp").forward(request,response);        
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	protected void buscaIFIportipo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String tipo = request.getParameter("tipo");
			IfiDTO bean = servicio.buscaIFIportipo(tipo);
			request.setAttribute("ifi", bean);
			request.getRequestDispatcher("/buscarIFI.jsp").forward(request,response);        
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void lista(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ArrayList<IfiDTO> lista = null;
		try {
			lista = servicio.listaIfi();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//Para que se pueda paginar se guarda en una sesion la lista
		request.getSession().setAttribute("ifis", lista);
		request.getRequestDispatcher("/buscarIFI.jsp").forward(request,response);
	}

		protected void buscarCodigo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			IfiDTO dto=null;
			try {
				String id =request.getParameter("id");
				System.out.println("llego parametro "+id);
				dto = servicio.buscaIFIporcodigo(id);
				request.setAttribute("ifi", dto);
				System.out.println("encontro bean "+dto.getCodigo());
				
				request.getRequestDispatcher("/GenerarPlazaVM.jsp").forward(request,response);        
//				request.getRequestDispatcher("/MantenerCliente.jsp").forward(request,response);   
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
}
